package com.nbjtjc.safe.service.edu;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduReassignmentMapper;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.edu.EduReassignment;
import com.nbjtjc.safe.util.MyShiroUtils;

@Service
public class EduReassignmentService {
	@Autowired
	EduReassignmentMapper eduReassignmentMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    
	  public Result add(EduReassignment eduReassignment){
	        Result result=new Result();
	        if(ObjectUtil.isNull(eduReassignment)
	        		||ObjectUtil.isNull(eduReassignment.getUserId())
	        		){
	            result.setFalse("对象和用户ID不能为空");
	            return result;
	        }

	        if(ObjectUtil.isNull(eduReassignment.getId())
	        		|| ObjectUtil.isNull(eduReassignmentMapper.selectByPrimaryKey(eduReassignment.getId()))){
	        	
	        	int  companeyId= MyShiroUtils.getCompanyId();
	        	eduReassignment.setCompanyId(companeyId);
	        	eduReassignment.setCompanyName(sysDeptMapper.findNameById(companeyId));
	        	eduReassignment.setCreateDate(new Date());
	        	eduReassignment.setCreateId(MyShiroUtils.getSessionUserId());
	        	
	        	int res=eduReassignmentMapper.insertSelective(eduReassignment);
	            
	        if(res==0){
	            result.setFalse("插入失败");
	            return result;

	        }
	        }else
	        {
	        	eduReassignment.setModifyDate(new Date());
	        	eduReassignment.setModifyId(MyShiroUtils.getSessionUserId());
	        	int res=eduReassignmentMapper.updateByPrimaryKeySelective(eduReassignment);
	        	 if(res==0){
	        		 result.setFalse("更新失败");
			         return result;

	        	 }
	        }
	        result.setData(eduReassignment);
	        return result;
	    }



	public Result delete(int id){
	    Result result=new Result();
	    if(ObjectUtil.isNull(id)){
	        result.setFalse("删除对象不能为空");
	        return result;
	    }
	    int res=eduReassignmentMapper.deleteByPrimaryKey(id);
	    if(res==0){
	        result.setFalse("删除失败");
	        return result;
	        }
	    return result;
	}
	
	//根据条件查找调查项目
	
	public Result find(EduReassignment eduReassignment) {
	    Result result = new Result();
	    PageHelper.startPage(eduReassignment.getPageCurrent(), eduReassignment.getPageSize());
	    eduReassignment.setCompanyId(MyShiroUtils.getCompanyId());

	    List<EduReassignment> eduReassignments = eduReassignmentMapper.search(eduReassignment);
	    result.setData(new PageInfo<>(eduReassignments));
	
	    return result;
	}
	
	public Result findDetail(EduReassignment eduReassignment) {
	    Result result = new Result();
	    PageHelper.startPage(eduReassignment.getPageCurrent(), eduReassignment.getPageSize());
	    eduReassignment.setCompanyId(MyShiroUtils.getCompanyId());

	    List<EduReassignment> eduReassignments = eduReassignmentMapper.searchDetail(eduReassignment);
	    result.setData(new PageInfo<>(eduReassignments));
	
	    return result;
	}
}
